#模型类 --- 对应关系的映射 映射成表的类型
from datetime import datetime

from exit import db

class Article(db.Model):
    __username__ = 'article' #数据库表名
    #用户id
    aid = db.Column(db.Integer, primary_key=True, autoincrement=True)
    #标题
    title = db.Column(db.String(100), nullable=False)
    #简略内容
    thumb_content = db.Column(db.String(150), nullable=False)
    #内容
    content = db.Column(db.String(10000), nullable=False)
    #发表时间
    pubdate = db.Column(db.DateTime, default=datetime.now())
    #浏览量
    looknum = db.Column(db.Integer, default=0)
    #点赞量
    likenum = db.Column(db.Integer, default=0)
    #外键
    uid = db.Column(db.Integer, db.ForeignKey('user.uid'), nullable=False)

    # 反向映射(若Comment表中传入数据了，则comments就不为空了，即文章有评论了）
    comments = db.relationship('Comment', backref='article')

    # 构造方法
    def __init__(self, title, content, uid):
        self.title = title
        self.thumb_content = content[:150] + '...' if len(content) > 150 else content
        self.content = content
        self.pubdate = datetime.now()
        self.looknum = 0
        self.likenum = 0
        self.uid = uid

#评论的模型
class Comment(db.Model):
    __tablename__ = 'comment'
    # 评论id
    cid = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 评论内容
    content = db.Column(db.String(255), nullable=False)
    # 评论的时间
    comdata = db.Column(db.DateTime, default=datetime.now())
    # 点赞量
    likenum = db.Column(db.Integer,default=0)
    # 谁评论的
    uid = db.Column(db.Integer, db.ForeignKey('user.uid'), nullable=False)
    # 评论的是哪篇文章
    aid = db.Column(db.Integer, db.ForeignKey('article.aid'), nullable=False)

    def __init__(self, content, uid, aid):
        self.content = content
        self.comdata = datetime.now()
        self.likenum = 0
        self.uid = uid
        self.aid = aid